﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wjtour.Domain;
using System.IO;
using System.Configuration;
using System.Text;
using Wjtour.BLL;
using System.Linq;

namespace Wjtour.Web.Front
{
    public class BasePage : System.Web.UI.Page
    {
        public string CurrentDate
        {
            get { return DateTime.Now.ToString("yyyy-MM-dd"); }
        }

        public string CurrentDay
        {
            get
            {
                switch (DateTime.Now.DayOfWeek)
                {
                    case DayOfWeek.Friday:
                        return "星期五";
                    case DayOfWeek.Monday:
                        return "星期一";
                    case DayOfWeek.Saturday:
                        return "星期六";
                    case DayOfWeek.Sunday:
                        return "星期日";
                    case DayOfWeek.Thursday:
                        return "星期四";
                    case DayOfWeek.Tuesday:
                        return "星期二";
                    case DayOfWeek.Wednesday:
                        return "星期三";
                    default:
                        return string.Empty;
                }
            }
        }

        public Wjtour.Domain.media FirstViedoWithImage
        {
            get
            {
                if (Session["FirstViedoWithImage"] == null)
                {
                    Wjtour.BLL.media_bll _media_bll = new media_bll();
                    var v = _media_bll.GetList(null, 1).FirstOrDefault();
                    Session["FirstViedoWithImage"] = v;
                    return v;
                }
                else
                {
                    return Session["FirstViedoWithImage"] as Wjtour.Domain.media;
                }
            }
        }

        public Wjtour.Domain.article FirstYoujiWithImage
        {
            get
            {
                if (Session["FirstYoujiWithImage"] == null)
                {
                    Wjtour.BLL.article_bll _article_bll = new article_bll();
                    var v = _article_bll.GetFirstArticleWithImage("6", 2);
                    Session["FirstYoujiWithImage"] = v;
                    return v;
                }
                else
                {
                    return Session["FirstYoujiWithImage"] as Wjtour.Domain.article;
                }
            }
        }

        public Wjtour.Domain.emagazine LatestMagazine
        {
            get
            {
                if (Session["LatestMagazine"] == null)
                {
                    emagazine_bll _emagazine_bll = new emagazine_bll();
                    var mag = _emagazine_bll.GetList(1).FirstOrDefault();
                    Session["LatestMagazine"] = mag;
                    return mag;
                }
                else
                {
                    return Session["LatestMagazine"] as Wjtour.Domain.emagazine;
                }
            }
        }
        public Wjtour.Domain.member CurrentMember
        {
            get
            {
                return Session["CurrentMember"] as Wjtour.Domain.member;
            }
        }

        public string BaiduKey
        {
            get
            {
                return ConfigurationManager.AppSettings["BAIDUKEY"];
            }
        }

        public string GetFileName(string type)
        {
            if (type == "guid")
                return Guid.NewGuid().ToString();
            else
                return string.Format("{0}{1}", DateTime.Now.ToString("yyyyMMddHHmmss"), DateTime.Now.Millisecond);
        }

        /// <summary>
        /// --0审核不通过，1等待审核，2审核通过
        /// </summary>
        /// <param name="audit"></param>
        /// <returns></returns>
        public string CovertAuditStatus(int audit)
        {
            switch (audit)
            {
                case 0:
                    return "审核不通过";
                case 1:
                    return "等待审核";
                case 2:
                    return "审核通过";
                default:
                    return string.Empty;
            }
        }
        protected string PagnationHtml { get; set; }
        /// <summary>
        /// 页数从1开始
        /// </summary>
        /// <param name="url"></param>
        /// <param name="currentpage"></param>
        /// <param name="totalpage"></param>
        /// <returns></returns>
        protected string GetPageHtml(string url, int currentpage, int totalpage)
        {
            int prepage = currentpage <= 1 ? 1 : currentpage - 1;
            int nextpage = totalpage > currentpage + 1 ? currentpage + 1 : totalpage;
            string thispageUrl = url.IndexOf("?") > -1 ? url + "&page=" + currentpage : url + "?page=" + currentpage;
            string prepageUrl = url.IndexOf("?") > -1 ? url + "&page=" + prepage : url + "?page=" + prepage;
            string nextpageUrl = url.IndexOf("?") > -1 ? url + "&page=" + nextpage : url + "?page=" + nextpage;
            string endpageUrl = url.IndexOf("?") > -1 ? url + "&page=" + totalpage : url + "?page=" + totalpage;

            int startpage = currentpage;
            int endpage = Math.Min(currentpage + 2, totalpage);
            startpage = Math.Max(endpage - 2, 1);

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<div class=\"pagecut\">");
            if (totalpage > 0)
            {
                sb.AppendLine("<span class=\"pagecount\">共" + totalpage + "页</span>");
                sb.AppendLine("<a href=\"" + url + "\"><span class=\"home\">首页</span></a>");
                sb.AppendLine("<a href=\"" + prepageUrl + "\"><span class=\"prepage\">上一页</span></a>");
                for (int i = startpage; i <= endpage; i++)
                {
                    if (i == currentpage)
                    {
                        sb.AppendLine("<a href=\"" + thispageUrl + "\"><span class=\"thispage\">" + currentpage + "</span></a>");
                    }
                    else
                    {
                        string pageUrl = url.IndexOf("?") > -1 ? url + "&page=" + i : url + "?page=" + i;
                        sb.AppendLine("<a href=\"" + pageUrl + "\"><span class=\"pagenum\">" + i + "</span></a>");
                    }
                }
                sb.AppendLine("<a href=\"" + nextpageUrl + "\"><span class=\"nextpage\">下一页</span></a>");
                sb.AppendLine("<a href=\"" + endpageUrl + "\"><span class=\"endpage\">末页</span></a>");
            }
            sb.AppendLine(" </div>");

            return sb.ToString();
        }

    }
}
